//
//  YDSettlementViewController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/1/16.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDSettlementViewController.h"
#import "YDPositionSettlementCell.h"
#import "YDPositionModel.h"
#import "YDTransactionDetailController.h"//交易详情
#import "YDVerticalStockBuyController.h"


@interface YDSettlementViewController ()<UITableViewDelegate , UITableViewDataSource , SYNoDataViewDelegate>

//当前页码
@property (nonatomic , assign) NSInteger currentPage;
//tableView
@property (nonatomic , weak) UITableView *tableView;

@property (nonatomic , strong) NSMutableArray *modelArray;

//无数据
@property (nonatomic , weak) SYNoDataView *dataView;
//网络占位图
@property (nonatomic, weak) SYNoNetView *noNetView;

//顶部安全区域
@property (nonatomic , assign) CGFloat safeTopH;
//底部安全区域
@property (nonatomic , assign) CGFloat safeBottomH;

@end

@implementation YDSettlementViewController

-(NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:mainGray];
    self.safeTopH = isPhoneX ? 84 : 64;
    self.safeBottomH = self.isHiddenTabbar ? 0 : (isPhoneX ? 83 : 49);
    //    DDLog(@"2222")
    [self tableView];
    [self setUpDataManger];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //    DDLog(@"已经出现2222")
    [self loadData];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    //    DDLog(@"已经消失2222")
}
- (void)removeNoNetView
{
    if (_noNetView != nil) {
        [_noNetView removeFromSuperview];
        _noNetView = nil;
    }
}

- (void)removeNodataView
{
    if (_dataView != nil) {
        [_dataView removeFromSuperview];
        _dataView = nil;
    }
}
/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        CGFloat tableH = LKScreenH - self.safeTopH - self.safeBottomH;
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, tableH) style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = [UIColor colorWithHexString:mainGray];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.scrollIndicatorInsets = tableView.contentInset;
        _tableView = tableView;
    }
    return _tableView;
}

/**
 刷新控件
 */
-(void)setUpDataManger
{
    MJGIFHeader *header = [MJGIFHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    header.lastUpdatedTimeLabel.hidden = YES ;
    header.stateLabel.hidden = YES ;
    header.label.hidden = YES;
    self.tableView.mj_header = header;
    //    [self.tableView.mj_header beginRefreshing];
    
    
    MJGIFFooter *footer = [MJGIFFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.stateLabel.hidden = YES;
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
}


/*******dataView********/
-(SYNoDataView *)dataView
{
    if (_dataView == nil) {
        CGFloat dataW = rate375(240);
        CGFloat dataX = (LKScreenW - dataW) * 0.5;
        CGFloat dataH = isPhoneX ? rateX812(276) : rate750(276);
        CGFloat dataY = isPhoneX ? rateX812(100) : rate750(100);
        CGRect frame = CGRectMake(dataX, dataY, dataW, dataH);
        //wuneirong   wuxinhao
        SYNoDataView *dataView = [SYNoDataView positionNoDataViewInitWithFrame:frame ImageString:@"wuneirong" buttonTitle:@"发布策略" IsCanSeeBuyButton:YES];
        dataView.delegate = self;
        dataView.isCanSeeBuyButton = YES;
        [self.tableView addSubview:dataView];
        _dataView = dataView;
    }
    return _dataView;
}
#pragma SYNoDataViewDelegate
-(void)noDataView:(SYNoDataView *)noDataView didClickSaleButton:(SYNoHeightedButton *)button
{
    YDVerticalStockBuyController *vc = [[YDVerticalStockBuyController alloc] init];
    vc.isNormal = YES;
    vc.isActivity = NO;
    vc.optionStyle = SYVerticalStockButtonOptionViewStyle0;
    vc.stockCode = @"000001";
    vc.stockName = @"平安银行";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(SYNoNetView *)noNetView
{
    if (_noNetView == nil) {
        SYNoNetView *noNetView = [[SYNoNetView alloc] init];
        [self.tableView addSubview:noNetView];
        
        [noNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(isPhoneX ? @(rateX812(40)) : @(rate750(40)));
        }];
        LK(weself)
        [noNetView setClickBlock:^(BOOL isClick) {
            [weself.tableView.mj_header beginRefreshing];
            
        }];
        _noNetView = noNetView;
    }
    return _noNetView;
}

//////*******加载数据********//////
-(void)loadData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(0) forKey:@"page"];
    [dict setObject:@(10) forKey:@"size"];
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:PositionSettlement WithSuccessBlock:^(NSDictionary *resultDict) {
        
        //        DDLog(@"resultDict = %@" , resultDict)
        
        [self.tableView.mj_header endRefreshing];
        
        if ([resultDict[@"code"] integerValue] == 200) {
            
            [self.modelArray removeAllObjects];
            self.currentPage = [resultDict[@"result"][@"number"] integerValue] + 1;
            self.modelArray = [YDPositionModel mj_objectArrayWithKeyValuesArray:resultDict[@"result"][@"content"]];
            
            if (self.modelArray.count <10) {
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshing];
            }
            
            if (self.modelArray.count) {
                [self removeNoNetView];
                [self removeNodataView];
            }else{
                [self removeNoNetView];
                [self removeNodataView];
                [self dataView];
                self.tableView.mj_footer.hidden = YES;
            }
            
            [self.tableView reloadData];
        }else{
            self.tableView.mj_footer.hidden = YES;
            
            if (self.modelArray.count) {
                [self.modelArray removeAllObjects];
                [self.tableView reloadData];
            }
            [self removeNoNetView];
            [self removeNodataView];
            [self noNetView];
            
        }
    } failBlock:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = YES;
        [self.tableView.mj_header endRefreshing];
        if (self.modelArray.count) {
            [self.modelArray removeAllObjects];
            [self.tableView reloadData];
        }
        
        [self removeNoNetView];
        [self removeNodataView];
        
        [self noNetView];
        
    }];
}

-(void)loadMoreData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.currentPage) forKey:@"page"];
    [dict setObject:@(10) forKey:@"size"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:PositionSettlement WithSuccessBlock:^(NSDictionary *resultDict) {
        //        DDLog(@"resultDict%@",resultDict)
        if ([resultDict[@"code"] integerValue] == 200) {
            
            self.currentPage = [resultDict[@"result"][@"number"] integerValue] + 1;
            
            NSArray *array = [YDPositionModel mj_objectArrayWithKeyValuesArray:resultDict[@"result"][@"content"]];
            [self.modelArray addObjectsFromArray:array];
            
            [self.tableView reloadData];
            
            if (array.count <10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            
        }else{
            [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"加载出错"];
            [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
            [self.tableView.mj_footer endRefreshing];
            
        }
    } failBlock:^(NSError *error) {
        //        DDLog(@"%@",error)
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"加载出错"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        [self.tableView.mj_footer endRefreshing];
    }];
}
#pragma UITableViewDelegate , UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(104) : rate750(104);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YDPositionSettlementCell *cell = [YDPositionSettlementCell cellWithTableView:tableView];
    YDPositionModel *model = self.modelArray[indexPath.row];
    cell.model= model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDPositionModel *model = self.modelArray[indexPath.row];
    YDTransactionDetailController *vc = [[YDTransactionDetailController alloc] init];
    vc.model = model;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
